<?php
namespace Admin\Controller;
class WorkflowRequestController extends BaseController {
	public function index(){
        $object=M('Workflowrequest');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		$list=$object->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        $this->display();
	}
    public function checkDiagram(){
        $id = I('get.id');
        $workflowid = I('get.workflowid');;
		if($id){
		    $temp = M('Workflowrequest')->where('id='.$id)->find();
		    $obj_rq = M('Workflowrequest');
            $obj_rq -> create($temp); //填充请求对象属性
            $workflowid = $obj_rq->workflowid;                                 //获得工作流id
			$this->assign('obj_rq',$obj_rq);                                                             //当前请求信息
		}
			$this->assign('object',M('Workflow')->where('id='.$workflowid)->find());                     //工作流对象
			$this->assign('nodelist',M('Workflownode')->where('workflowid='.$workflowid)->select());     //节点对象列表
            $this->display('checkDiagram');
    }
	public function indexDB(){
        $User = session("User");
        $object=M('Workflowrequest');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		
		$list=$object
		->field('bs_Workflowrequest.*,b.name as wfname,c.name as ndname,d.nickname as crname,e.nickname as cuname')
		->join('LEFT JOIN bs_workflow b       ON bs_Workflowrequest.workflowid = b.id')
		->join('LEFT JOIN bs_workflownode c   ON bs_Workflowrequest.currentnodeid = c.id')
		->join('LEFT JOIN bs_user d           ON bs_Workflowrequest.creater = d.id')
		->join('LEFT JOIN bs_user e           ON bs_Workflowrequest.currenter = e.id')
		->where
		    ('bs_Workflowrequest.currenter='.$User[id].' and c.type<>2')
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        //$this->display('WorkflowRequest:index');
        $this->display();
	}
	public function indexMy(){
        $User = session("User");
        $object=M('Workflowrequest');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		
		$list=$object
		->field('bs_Workflowrequest.*,b.name as wfname,c.name as ndname,d.nickname as crname,e.nickname as cuname')
		->join('LEFT JOIN bs_workflow b       ON bs_Workflowrequest.workflowid = b.id')
		->join('LEFT JOIN bs_workflownode c   ON bs_Workflowrequest.currentnodeid = c.id')
		->join('LEFT JOIN bs_user d           ON bs_Workflowrequest.creater = d.id')
		->join('LEFT JOIN bs_user e           ON bs_Workflowrequest.currenter = e.id')
		->where
		    ('bs_Workflowrequest.creater='.$User[id])
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        //$this->display('WorkflowRequest:index');
        $this->display();
	}
	public function indexGd(){
        $User = session("User");
        $object=M('Workflowrequest');
        import('ORG.Util.Page');			//Page类的引入
        $count=$object->where($where)->count(); //获取符合条件的数据总数count
		$Page=new \Org\Util\Page($count, 10);			//实例化page类，传入数据总数和每页显示10条内容
		$show=$Page->show();
		
		$list=$object
		->field('bs_Workflowrequest.*,b.name as wfname,c.name as ndname,d.nickname as crname,e.nickname as cuname')
		->join('LEFT JOIN bs_workflow b       ON bs_Workflowrequest.workflowid = b.id')
		->join('LEFT JOIN bs_workflownode c   ON bs_Workflowrequest.currentnodeid = c.id')
		->join('LEFT JOIN bs_user d           ON bs_Workflowrequest.creater = d.id')
		->join('LEFT JOIN bs_user e           ON bs_Workflowrequest.currenter = e.id')
		->where
		    ('bs_Workflowrequest.currenter='.$User[id].' and c.type=2')
		->limit($Page->firstRow.','.$Page->listRows)->select();
		
        $this->assign('list',$list);
        $this->assign("page",$show);
        //$this->display('WorkflowRequest:index');
        $this->display();
	}
    public function editUI(){
        $id = I('get.id');
        $workflowid = I('get.workflowid');
		if($id){//修改
		    $object = M('Workflowrequest') -> where('id='.I('get.id'))->find();                             //获得请求对象
            $workflowid = $object[workflowid];                                                              //从请求对象获得工作流id
            $object_wf = M('Workflow') -> where('id='.$workflowid)->find();                                 //根据工作流id获得表单id
            $object_fm = M('Form') -> where('id='.$object_wf[formid])->find();                              //根据表单id获得表单对象
			$this->assign('object_currentnode',M('Workflownode') -> where('id='.$object[currentnodeid])->find());
			$this->assign('object_fm',M($object_fm[formname]) -> where('requestid='.I('get.id'))->find());  //发送表单数据对象
        echo '<br>'.'修改=$$object[currentnodeid]====='.$object[currentnodeid];
		    $object_node = M('Workflownode')->where('id='.$object[currentnodeid])->find();                  //根据节点id获得节点对象
		    echo '<br>$object_node'.$object_node[currentnodeid];
		    echo '<br>$object_node_type'.$object_node[type];

		    $this->assign('cnode',$object_node);
		    $this->assign('object',$object);
		    
		    $this->assign('qzobj',M('Workflowrequestnode')->where('requestid='.$id.' and nodeid='.$object[currentnodeid])->find());   //获得当前签字意见
		    $this->assign('qzList',M('Workflowrequestnode')->where('requestid='.$id.' and nodeid<>'.$object[currentnodeid])           //获得其他签字意见
		        ->order('id desc')
		        ->field('bs_Workflowrequestnode.*,b.nickname as uname,c.name as nname')
		        ->join('LEFT JOIN bs_user b ON bs_Workflowrequestnode.creater = b.id')
		        ->join('LEFT JOIN bs_workflownode c ON bs_Workflowrequestnode.nodeid = c.id')
		        ->select());                                     //所有签字意见;
		}else{//新建
            $workflowid = I('get.workflowid');
		    $object_node = M('Workflownode')->where('type=0 and workflowid='.$workflowid)->find();          //创建节点，根据流程id获得节点id
            $object_wf = M('Workflow') -> where('id='.$workflowid)->find();                                 //根据工作流id获得表单id
		}
        $this->assign('rwList',M('Workflownodefield')->where('nodeid='.$object_node[id])->select()); 
        //echo M('Workflownodefield')->getLastSql();       //发送字段读写权限
		$this->assign('fieldList',M('Formfield')->where('formid='.$object_wf[formid])->select());           //发送表单字段信息
		$this->assign('object_wf',$object_wf);                                                            //发送工作流信息
		
        $this->display('editUI');
    }
    public function editOperation(){
          /*********************取参数***************************/
          $User = session("User");              //用户信息
          $id = I('post.id');                   //请求id
          $name = I('post.name');               //请求名称
          $method = I('post.method');           //操作类型
          $remark = I('post.remark');           //签字意见
          /*********************取参数***************************/
          
          $obj_rq = M('Workflowrequest');   //创建请求对象

          if(!$id){//如果是新建
              $workflowid = I('post.workflowid');                               //获取工作流id
              $Object_wf = M('Workflow')->where('id='.$workflowid)->find();     //根据流程id找到表单id
              $Object_fm = M('Form')->where('id='.$Object_wf[formid])->find();  //根据表单id获得表单名
              /*************************请求***********************************/
              $obj_rq -> create();                                                                      //填充页面参数
              $obj_rq -> id = null;                                                                     //重置请求id
              $Object_node = M('Workflownode')->where('type=0 and workflowid='.$workflowid)->find();    //获取第一个节点对象
              $obj_rq -> currentNodeid = $Object_node[id];                                              //设置请求的当前节点
              $obj_rq -> creater = $User[id];                                                           //设置请求的创建者
              $obj_rq -> currenter = $User[id];                                                         //设置当前节点操作者
              $requestId = $obj_rq -> add();                                                            //添加请求对象,返回对象id
              
              echo '<br>requestId='.$requestId;
              
              $obj_rq -> create(M('Workflowrequest')->where('id='.$requestId)->find());
              //$obj_rq->id = $requestId;                                                                 //获得请求id
              
              /*************************表单***********************************/
              $obj_fm = M($Object_fm[formname]);                                  //根据表单名新建表单对象
              $obj_fm -> create();                                                //填充页面参数
              $obj_fm -> id=null;                                                 //重置id
              $obj_fm -> creater=$User[id];                                       //重置id
              $obj_fm -> requestId = $requestId;                                  //设置表单的requestid
              $obj_fm -> add();                                                   //添加表单对象
              
              /*************************节点***********************************/
              $obj_rqnode = M('Workflowrequestnode');
              $obj_rqnode->create();
              $obj_rqnode->requestId = $requestId;                      //获得请求id
              $obj_rqnode->nodeid = $Object_node[id];                   //获得请求id
              $obj_rqnode->creater = $User[id];                        //获得请求id
              $obj_rqnode->remark = $remark;                             //获得请求id
              $obj_rqnode->createtime = time();                         //获得请求id
              $obj_rqnode->add();                                   //添加表单信息
          }else{//如果是修改
              //1.保存表单信息
              //2.保存节点信息
              $obj_rq -> create(M('Workflowrequest')->where('id='.$id)->find()); //填充请求对象属性
              $obj_rq -> name = $name;                                           //设置请求主题
              $workflowid = $obj_rq->workflowid;                                 //获得工作流id            
              echo $obj_rq->id.'<br>';    
              echo M('Workflowrequest')->where('id='.$id)->find()->currentnodeid.'<br>';  
              echo $obj_rq->currenter.'<br>';  
              echo $obj_rq.currentnodeid.'<br>';  
              echo $obj_rq->workflowid.'<br>';
              $Object_node = M('Workflownode')->where('id='.($obj_rq->currentnodeid))->find();    //获得当前节点对象
              //$obj_rq -> save();                                 //保存请求信息

              /*************************表单***********************************/
              $Object_wf = M('Workflow')->where('id='.$workflowid)->find();     //根据流程id找到表单id
              $Object_fm = M('Form');
              $Object_fm ->where('id='.$Object_wf[formid])->find();             //根据表单id获得表单名

              $obj_fm2 = M($Object_fm->formname)->where("requestId=".$id)->find();
              $obj_fm  = M($Object_fm->formname);                                //根据表单名新建表单对象
              $obj_fm->create();                                                //填充表单对象属性

//               if($obj_fm2==$obj_fm){ echo '相等';}
//               if($obj_fm2==$obj_fm){ echo '相等';}
              echo '<br>'.$obj_fm->id.'___'.$obj_fm2[id];
              echo '<br>'.$obj_fm->creater.'___'.$obj_fm2[creater];;
              echo '<br>'.$obj_fm->requestId.'___'.$obj_fm2[requestid];
              
              $obj_fm->id = $obj_fm2[id];                                       //设置表单的原id
              $obj_fm->creater = $obj_fm2[creater];                             //设置表单的原creater
              $obj_fm->requestId = $obj_fm2[requestid];                         //设置表单的原requestId

              $obj_fm->save();
              echo '___'.M($Object_fm->formname)->getLastSql().'___';
              
              /*************************节点***********************************/
              $obj_rqnode = M('Workflowrequestnode');
              $temp = M('Workflowrequestnode')->where('requestid='.$id.' and nodeid='.$Object_node[id])->find();
              if($temp){    //存在请求节点信息 
                $obj_rqnode->create($temp);
                $obj_rqnode->remark = $remark;                             //获得请求id
                $obj_rqnode->creater = $User[id];                          //获得请求id
                $obj_rqnode->createtime = time();                          //获得请求id
                $obj_rqnode->save();                                       //修改表单建模对象
              }else{
                $obj_rqnode->create();
                $obj_rqnode->id = null;                                    //重置id
                $obj_rqnode->requestId=$id;
                $obj_rqnode->nodeid=$Object_node[id];
                $obj_rqnode->remark = $remark;                             //获得请求id
                $obj_rqnode->creater = $User[id];                          //获得请求id
                $obj_rqnode->createtime = time();                          //获得请求id
                $obj_rqnode->add();                                        //添加表单建模对象
              }
          }
          
          //请求信息换节点
          if($method=='submit'){  //提交
              $Object_nextnode = M("Workflownode")->where("id=".$Object_node[nextnodeid])->find();  //找到下个节点信息
              
              if($Object_nextnode[handlertype]=='0'){//创建人
                  $currenter = $obj_rq -> creater;                               //设置当前操作人为创建人
              }else if($Object_nextnode[handlertype]=='1'){//创建人上级
                  echo $obj_rq -> creater.'---------';
                  $obj_cr = M('User')->where('id='.$obj_rq -> creater)->find(); 
                  if($obj_cr[pid]){
                    $currenter = $obj_cr[pid];                              //设置当前操作人为创建人上级
                  }else{
                      echo "<script>alert('流程流转错误，找不到下个节点操作者');</script>";
                      return;
                  }
              }else if($Object_nextnode[handlertype]=='2'){//人力资源
                  $currenter = $Object_nextnode[handlerobject1];        //设置当前操作人为人力资源字段
              }
              $obj_rq->currentNodeid = $Object_node[nextnodeid];        //设置当前节点id
              $obj_rq->prevNodeid = $Object_node[id];                   //设置上一个节点id
              $obj_rq->currenter = $currenter;                          //设置当前操作者
              $obj_rq->save();          //保存当前节点数据  设置requestId nodeid remark creater createtime
          }else if($method=='reject'){
              
          }
	      $this->success('操作成功',U('indexDB'));
    }
    public function removeOperation(){
		$Object = M("Workflowrequest");
		if($Object->delete(I('get.id'))){
			$this->success('删除成功',U('index'));
		}else{
			dump($Object->_sql());
			$this->error('删除失败');
		}
    }
}